package com.todo.event.activity.application.handler.activitytouch;

import com.alibaba.fastjson.JSONObject;
import com.todo.event.activity.application.entity.context.ActivityTouchContext;
import com.todo.event.activity.common.enums.EventActivityExecuteTouchEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @author azhebuxing
 * @date 2025/1/13 20:38
 * @description
 */
@Slf4j
public abstract class AbstractTouchHandle {

    public boolean handle(ActivityTouchContext context) {
        if (context == null) {
            return true;
        }
        if (match(context)) {
            boolean notNeedNextHandle = preHandle(context);
            if (notNeedNextHandle) {
                log.info("AbstractTouchHandle#handle preHandle success touch:【{}】 context:【{}】", getTouch(), JSONObject.toJSONString(context));
                return true;
            }
            try {
                Boolean result = doHandle(context);
                log.info("AbstractTouchHandle#handle execute success touch:【{}】 context:【{}】 result:【{}】", getTouch(), JSONObject.toJSONString(context), result);
                return result;
            } catch (Exception ex) {
                log.error("AbstractTouchHandle#handle execute error touch:【{}】 context:【{}】", getTouch(), JSONObject.toJSONString(context), ex);
            }
        }
        return true;
    }

    public boolean match(ActivityTouchContext context) {
        return true;
    }

    protected abstract boolean preHandle(ActivityTouchContext context);

    protected abstract Boolean doHandle(ActivityTouchContext context);

    protected abstract EventActivityExecuteTouchEnum getTouch();
}
